package edu.pku.ss.taas.cloud.monitor.lightweight;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;


public class MonitorClientManager {

	// ����ȫ�ֵ�Ψһʵ��
	private static MemCachedClient mcc = new MemCachedClient();

	private static MonitorClientManager monitorServerManager = new MonitorClientManager();

	// �����뻺������������ӳ�
	static {
		// �������б����Ȩ��
		String serverIp = MonitorClientConfigureManager.getStringConfigure(Constants.MONITOR_SERVER_HOST_IP);
		String serverPort = MonitorClientConfigureManager.getStringConfigure(Constants.MONITOR_SERVER_HOST_PORT);
		String[] servers = { serverIp+":"+ serverPort};
		Integer[] weights = { 3 };

		// ��ȡsocke���ӳص�ʵ�����
		SockIOPool pool = SockIOPool.getInstance();

		// ���÷�������Ϣ
		pool.setServers(servers);
		pool.setWeights(weights);

		// ���ó�ʼ��������С������������Լ������ʱ��
		pool.setInitConn(1);
		pool.setMinConn(1);
		pool.setMaxConn(3);
		pool.setMaxIdle(1000 * 60 * 60 * 6);

		// �������̵߳�˯��ʱ��
		pool.setMaintSleep(30);

		// ����TCP�Ĳ������ӳ�ʱ��
		pool.setNagle(false);
		pool.setSocketTO(3000);
		pool.setSocketConnectTO(0);

		// ��ʼ�����ӳ�
		pool.initialize();

	}

	/**
	 * �����͹��췽����������ʵ��
	 * 
	 */
	protected MonitorClientManager() {

	}

	/**
	 * ��ȡΨһʵ��.
	 * 
	 * @return
	 */
	public static MonitorClientManager getInstance() {
		return monitorServerManager;
	}

	public boolean keyExists(String arg0) {
		return mcc.keyExists(arg0);
	}

	public boolean delete(String arg0) {
		return mcc.delete(arg0);
	}

	public boolean flushAll() {
		return mcc.flushAll();
	}

	/**
	 * ���һ��ָ����ֵ��������.
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean add(String key, Object value) {
		return mcc.add(key, value);
	}

	public boolean add(String key, Object value, Date expiry) {
		return mcc.add(key, value, expiry);
	}

	public boolean replace(String key, Object value) {
		return mcc.replace(key, value);
	}

	public boolean replace(String key, Object value, Date expiry) {
		return mcc.replace(key, value, expiry);
	}

	/**
	 * ���ָ���Ĺؼ��ֻ�ȡ����.
	 * 
	 * @param key
	 * @return
	 */
	public Object get(String key) {
		return mcc.get(key);
	}

	public static void main(String[] args) {
		MonitorClientManager cache = MonitorClientManager.getInstance();
		String  hostIp = MonitorUtil.getLocalIP();
		System.out.println("begin to get monitor data from server");
		int i = 10;
		while(i-->0){
			Map monitorDatas = (Map)cache.get(hostIp);
			System.out.println("get monitor data batch:"+(10-i));
			System.out.println("cpu usage:"+monitorDatas.get(Constants.CPU_USAGE));
			System.out.println("mem usage:"+monitorDatas.get(Constants.MEM_USAGE));
			System.out.println("disk usage:"+monitorDatas.get(Constants.DISK_USAGE));
			
			try {
				Thread.currentThread().sleep(1000*5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
}